import React, { useEffect } from 'react'
import { connect, useDispatch, history } from 'umi'
import { actions } from './model'
import { Toast } from 'antd-mobile'
import { PostSumbitIdea, PostcreateOrder } from '@/api'
import '@/assets/Cart/index.less'
function Cart(props: any) {

  const dispatch = useDispatch()

  useEffect(() => {
    dispatch(actions.getCartList())
    dispatch(actions.cartPrice())
  }, [])

  let { Cart: { Cartlist, sumprice, sumcheck } } = props
  // console.log('props:购物车页面', props)  
  return (
    <div className='cart'>
      <ul>
        {
          Cartlist.length && Cartlist.map((item: any, index: any) => {
            return <li key={index}>
              <p className='cart_check'><input type="checkbox" checked={item.checkflag} onChange={(e: any) => {
                let checke = e.target.checked
                dispatch(actions.itemCheck({ item, checke }))
                dispatch(actions.cartPrice())
              }} /></p>
              <p className='cart_title'>
                <img src={item.goods_image} alt="" />
                <span className='cart_title_span'>
                  <span>{item.goods_name}</span>
                  <span><a>{item.goods_price}</a>元/件</span>
                </span>
              </p>
              <p className='cart_price'>
                <button onClick={() => {
                  dispatch(actions.cartJian(item.id))
                  dispatch(actions.cartPrice())
                }}>-</button>
                {item.buy_num}
                <button onClick={() => {
                  dispatch(actions.cartJia(item.id))
                  dispatch(actions.cartPrice())
                }}>+</button>
              </p>
            </li>
          })
        }
      </ul>
      <p className='cart_sumbtn'>
        <span className='cart_sumbtn_one'>
          <input type="checkbox" checked={sumcheck} onChange={(e) => {
            dispatch(actions.sumCheck(e.target.checked))
            dispatch(actions.cartPrice())
          }} />全选
          </span>
        <span className='cart_sumbtn_two'>
          <span>合计：</span><a>¥{sumprice}</a>
        </span>
        <button onClick={() => {
          let arr: any = []
          Cartlist.filter((item: any) => item.checkflag).forEach((item: any) => {
            arr.push(item.id)
          })
          console.log('arr:', arr.length)
          // PostSumbitIdea({idea_content: 'dddddd'}).then(res => {
          //   console.log('res:', res)
          // })
          // 提交订单接口 （总价、）
          if(arr.length) {
            console.log('sumprice:', sumprice)
            PostcreateOrder({
              price: sumprice,
              car_id: arr,
              type: 1,
            }).then(res => {
              console.log('创建订单接口', res)
              if ((res as any).code === 1) {
                const { order_id } = res.data;
                Toast.success((res as any).msg, 1)
                history.push(`/orderConfirm?order_id=${order_id}`)
              } else {
                Toast.fail((res as any).msg, 1)
              }

            })
          } else {
            Toast.fail('请先选中商品', 1)
          }
        }}>
          提交订单
          </button>
      </p>
    </div>
  )
}

export default connect(state => {
  return { ...state }
})(Cart)
